package cn.com.dbappsecurity.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import cn.com.dbappsecurity.dao.UserMapper;
import cn.com.dbappsecurity.entity.User;
import cn.com.dbappsecurity.frame.Constant;
import cn.com.dbappsecurity.frame.annotations.Transactional;
import cn.com.dbappsecurity.frame.exception.FrameException;
import cn.com.dbappsecurity.service.IUser;
import cn.com.dbappsecurity.utils.SecretUtil;
import cn.com.dbappsecurity.utils.ValidateUtil;

public class UserImpl extends BaseServiceImpl implements IUser{

	private UserMapper userMapper = getMapper(UserMapper.class);
	
	/**
	 * 插入一个用户
	 * @param name
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional
	public boolean insert(String name,String password)throws Exception{
		if(!ValidateUtil.checkName(name) || !ValidateUtil.checkPassword(password)){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		int num = userMapper.userExist(name);
		if(num > 0){
			throw new FrameException(FrameException.TYPE_USER_EXIST);
		}
		User user = new User();
		user.setName(name);
		user.setPassword(SecretUtil.digestSha1(password));
		num = userMapper.insert(user);
		return num == 1;
	}

	/**
	 * 根据ID删除用户
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional
	public boolean delete(Integer[] id)throws Exception{
		if(id == null){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		int num = userMapper.delete(id);
		return num == id.length;
	}
	public static void main(String[] args) throws Exception {
		System.out.println(SecretUtil.digestSha1("123456"));
	}
	/**
	 * 用户登录
	 * @param session
	 * @param name
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@Override
	public boolean login(HttpSession session,String name,String password)throws Exception{
		if(session == null || !ValidateUtil.checkName(name) || !ValidateUtil.checkPassword(password)){
			throw new FrameException(FrameException.TYPE_CHECK_ERROR);
		}
		User user = userMapper.login(name,SecretUtil.digestSha1(password));
		if(user == null){
			throw new FrameException(FrameException.TYPE_CHECK_ERROR);
		}
		//修改最后登录时间
		user.setLoginTime(new Date());
		userMapper.update(user);
		session.setAttribute(Constant.AUTH_KEY,user);
		//session.setAttribute(AUTH_PRIVILEGE,queryPrivilege(user.getId()));
		return true;
	}
	
	/**
	 * 修改用户
	 * @param id
	 * @param name
	 * @param password
	 * @param loginTime
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional
	public boolean update(int id,String name,String password,String loginTime)throws Exception{
		if(id < 1){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		if(name != null && !ValidateUtil.checkName(name)){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		if(password != null && !ValidateUtil.checkPassword(password)){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		if(loginTime != null && !ValidateUtil.checkDate(loginTime)){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		
		User user = new User();
		user.setId(id);
		user.setName(name);
		if(password != null){
			user.setPassword(SecretUtil.digestSha1(password));
		}
		if(loginTime != null){
			user.setLoginTime(new SimpleDateFormat("yyyy-MM-dd").parse(loginTime));
		}
		
		int num = userMapper.update(user);
		return num == 1;
	}
	
	/**
	 * 用户查询
	 * @param name
	 * @param createTimeFrom
	 * @param createTimeTo
	 * @param loginTimeFrom
	 * @param loginTimeTo
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<User> search(String name,String createTimeFrom,String createTimeTo,
			String loginTimeFrom,String loginTimeTo,int page,int rows,String sort,String order)throws Exception{
		if(rows > Constant.MAX_PAGE_AVG){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		return userMapper.search(name, createTimeFrom, createTimeTo, loginTimeFrom, loginTimeTo, getLimit(page,rows), rows,sort,order);
	}
	
	/**
	 * 用户查询总条数
	 * @param name
	 * @param createTimeFrom
	 * @param createTimeTo
	 * @param loginTimeFrom
	 * @param loginTimeTo
	 * @return
	 */
	@Override
	public int searchCount(String name,String createTimeFrom,String createTimeTo,
			String loginTimeFrom,String loginTimeTo)throws Exception{
		return userMapper.searchCount(name, createTimeFrom, createTimeTo, loginTimeFrom, loginTimeTo);
	}
}
